package com.voicechat.firstmet.imcontrol;


import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.tencent.TIMManager;
import com.tencent.TIMMessage;
import com.tencent.TIMMessageListener;
import com.tencent.TIMUserStatusListener;
import com.tencent.av.utils.PhoneStatusMonitor;
import com.voicechat.firstmet.App;

import java.util.List;
import java.util.Observable;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * 消息通知事件，上层界面可以订阅此事件
 */
public class MessageEvent extends Observable implements TIMMessageListener,PhoneStatusMonitor.PhoneStatusListener,TIMUserStatusListener {
    private volatile static MessageEvent instance;
    private static TelephonyManager manager = null;
    private String TAG = "MessageEvent";
    public static final String SELECT_IS_VOICE = "select_is_voice";//收到语聊邀请消息
    public static final String SELECT_IS_VOICE_ANSWER = "select_is_voice_answer";//对方接听了语音聊天
    public static final String SELECT_IS_VOICE_HANGUP = "select_is_voice_hang_up";//对方挂断了语音聊天
    public static final String SELECT_IS_VOICE_END = "select_is_voice_end";//语音聊天结束
    public static final String WS_MANAGER = "ws_error";//长链接错误，需重连
    public static final String WS_Disconnected = "ws_disconnected";//长链接断开，需重连
    public static final String WS_HEAD = "ws_head";//长链接心跳
    private Context context;

    private MessageEvent(){
       // manager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
        //注册消息监听器
        TIMManager.getInstance().addMessageListener(this);
       // TIMManager.getInstance().setUserStatusListener(this);
    }

    public static MessageEvent getInstance(){
        if (instance == null) {
            synchronized (MessageEvent.class) {
                if (instance == null) {
                    instance = new MessageEvent();
                    //获取电话服务
                    manager = (TelephonyManager) App.getContext().getSystemService(TELEPHONY_SERVICE);
                }
            }
        }
        return instance;
    }

    @Override
    public boolean onNewMessages(List<TIMMessage> list) {
        //获取会话未读数
        for (TIMMessage item:list){
            setChanged();
            notifyObservers(item);
        }
        return false;
    }

    /**
     * 主动通知新消息
     */
    public void onNewMessage(TIMMessage message){
        setChanged();
        notifyObservers(message);
    }
    /**
     * 清理消息监听
     */
    public void clear(){
        instance = null;
    }
    /**
    * @method
    * @description 当前手机电话状态监听
    * @date: 2018/11/12 17:24
    * @author: ljp
    * @return
    */
    @Override
    public void onCallStateChanged(boolean b) {
        Log.i(TAG, "onCallStateChanged: -----"+b);

    }


    @Override
    public void onForceOffline() {
        Log.i(TAG, "onForceOffline: --------");

    }

    @Override
    public void onUserSigExpired() {
        Log.i(TAG, "onUserSigExpired: ---------");

    }
}
